


import 'package:flutter/material.dart';

class SignaturePainter extends CustomPainter{
  final List<Offset> points;

  SignaturePainter(this.points);
  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    var paint=new Paint()
    ..color=Colors.black
    ..strokeCap=StrokeCap.round
    ..strokeWidth=5.0;
    for(int i=0;i<points.length-1;i++){
      if(points[i]!=null&&points[i+1]!=null){
        canvas.drawLine(points[i], points[i+1], paint);
      }
    }
  }

  @override
  bool shouldRepaint(SignaturePainter oldDelegate) {
    // TODO: implement shouldRepaint
    return oldDelegate.points!=points;
  }

}

class Signature extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new SignatureState();
  }

}

class SignatureState extends State<Signature>{
  List<Offset> _points=<Offset>[];
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    print("SignatureState");
    return new GestureDetector(
      onPanUpdate:(DragUpdateDetails details){
        setState(() {
          RenderBox referenceBox=context.findRenderObject();
          Offset localPosition=referenceBox.globalToLocal(details.globalPosition);
          _points=new List.from(_points)..add(localPosition);
        });
        print("onPanUpdate");
      },
      onPanEnd: (DragEndDetails details)=> _points.add(null),
      child: new CustomPaint(painter:new SignaturePainter(_points),size: MediaQuery.of(context).size,),
      //child: new Text("我是body"),
    );
  }


}

class DemoApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    print("hello world");
    // TODO: implement build
    return new MaterialApp(
      title: "这是标题",
      home:new Scaffold(
        appBar: AppBar(title: new Text("画板"),),
        body:new Scaffold(body:new Signature())

      ),
    );
  }
}

